﻿using System;
using System.Text;
using NUnit.Framework;

namespace SharePoint.Orm.Helpers {
	[TestFixture]
	public class TypeSystemTest : TestBase {
		[Test]
		public void TestCast_ObjectToString() {
			object obj = "test";
			string ret = TypeSystem.Cast<string>(obj);
			Assert.AreEqual("test", ret);
		}

		[Test]
		public void TestCast_NullToInt() {
			int ret = TypeSystem.Cast<int>(null);
			Assert.AreEqual(default(int), ret);
		}

		[Test]
		public void TestCast_NullToNullableInt() {
			int? ret = TypeSystem.Cast<int?>(null);
			Assert.AreEqual(null, ret);
		}

		[Test]
		public void TestCast_DoubleToInt() {
			const double val = 10.0;
			int ret = TypeSystem.Cast<int>(val);
			Assert.AreEqual(10, ret);
		}

		[Test]
		public void TestCast_DoubleToNullableInt() {
			const double val = 10.0;
			int? ret = TypeSystem.Cast<int?>(val);
			Assert.AreEqual(10, ret);
		}

		[Test]
		public void TestTypeToCodeString() {
			Assert.AreEqual("int", TypeSystem.TypeToCodeString(typeof(int)));
		}

		[Test]
		public void TestTypeToCodeString_Nullable() {
			Assert.AreEqual("int?", TypeSystem.TypeToCodeString(typeof(int?)));
		}

		public class MyGeneric<X, Y, Z> {
			public X x;
			public Y y;
			public Z z;
		}

		[Test]
		public void TestTypeToCodeString_Generic() {
			Assert.AreEqual(
				"SharePoint.Orm.Helpers.TypeSystemTest.MyGeneric<int?, string, SharePoint.Orm.Helpers.TypeSystemTest.MyGeneric<System.Type, bool, System.Text.StringBuilder>>",
				TypeSystem.TypeToCodeString(typeof(MyGeneric<int?, string, MyGeneric<Type, bool, StringBuilder>>)));
		}
	}
}